package rps.client.ai;

import static rps.game.data.FigureKind.FLAG;
import static rps.game.data.FigureKind.PAPER;
import static rps.game.data.FigureKind.ROCK;
import static rps.game.data.FigureKind.SCISSORS;
import static rps.game.data.FigureKind.TRAP;

import java.rmi.RemoteException;
import java.util.Collection;
import java.util.Iterator;

import rps.client.GameListener;
import rps.game.Game;
import rps.game.Movement;
import rps.game.data.Figure;
import rps.game.data.FigureKind;
import rps.game.data.Move;
import rps.game.data.Player;

/**
 * This class contains a very basic AI, that allows to play a game against it.
 * The main benefit is to be able to test the UI.
 */
public class BasicAi implements GameListener {

	private Player player = new Player("Basic AI");
	private Game game;
	private final FigureKind[] initAs = new FigureKind[] {null, null, null, null, null, null, null,
			null, null, null, null, null, null, null,
			null, null, null, null, null, null, null,
			null, null, null, null, null, null, null, PAPER, PAPER, PAPER, PAPER, ROCK, ROCK, ROCK,
		FLAG, TRAP, ROCK, SCISSORS, SCISSORS, SCISSORS, SCISSORS,};
	
	private FigureKind randomFigure(){
		int randomNumber = 1+(int)(Math.random()*3);
		FigureKind myFigure;
		switch(randomNumber){
		case 1:
			myFigure = FigureKind.ROCK;
			break;
		case 2:
			myFigure = FigureKind.PAPER;
			break;
		case 3:
			myFigure = FigureKind.SCISSORS;
			break;
		default:
			myFigure = FigureKind.ROCK;
		}
		return myFigure;
	}
	
	@Override
	public void chatMessage(Player sender, String message) throws RemoteException {
		// TODO Auto-generated method stub
		if (!player.equals(sender)) {
			game.sendMessage(player, ":-)");
		}
	}

	@Override
	public void provideInitialAssignment(Game game) throws RemoteException {
		this.game = game;
		game.setInitialAssignment(player, initAs);
	}

	@Override
	public void provideInitialChoice() throws RemoteException {
		game.setInitialChoice(player, randomFigure());
	}

	@Override
	public void startGame() throws RemoteException {
		System.out.println("Ui gestartet");
	}

	@Override
	public void provideNextMove() throws RemoteException {
		Collection<Move> moves = new Movement(game, player).getPosMoves();
		Move nextMove = null;
		int randomNumber = 1+(int)(Math.random()*moves.size());
		Iterator<Move> it = moves.iterator();
		for (int i = 0; i < randomNumber; i++) {
			nextMove = it.next();
		}
		System.out.println("AI zieht von " + nextMove.getFrom() + " nach " + nextMove.getTo());
		game.move(player, nextMove.getFrom(), nextMove.getTo());
	}
	
	@Override
	public void figureMoved() throws RemoteException {
		// TODO Auto-generated method stub

	}

	@Override
	public void figureAttacked() throws RemoteException {
		// TODO Auto-generated method stub

	}

	@Override
	public void provideChoiceAfterFightIsDrawn() throws RemoteException {
		game.setUpdatedKindAfterDraw(player, randomFigure());
	}

	@Override
	public void gameIsLost() throws RemoteException {
		game.sendMessage(player, "asshole");
	}

	@Override
	public void gameIsWon() throws RemoteException {
		game.sendMessage(player, "Yipiyayeah motherfucker");
	}

	@Override
	public void gameIsDrawn() throws RemoteException {
		game.sendMessage(player, "Please Restart ;D");

	}

	@Override
	public String toString() {
		return player.getNick();
	}

	@Override
	public Player getPlayer() throws RemoteException {
		return this.player;
	}
}